package com.heima.user.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.cache.CacheService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.IApUserFollowService;
import com.heima.utils.common.AppThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.Log;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;


@Service
@RequiredArgsConstructor
@Slf4j
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements IApUserFollowService {

//    private final ApUserFollowMapper apUserFollowMapper;
//    private final ApUserMapper apUserMapper;
//    private final ApUserFanMapper apUserFanMapper;
    private final CacheService cacheService;

    /**
     * 用户关注h和取消关注
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //校验参数
        if (ObjectUtils.isEmpty(dto) || dto.getOperation() < 0 || dto.getOperation() > 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //必须是登录状态下才能关注
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //若userId为空就不能关注
        if (StringUtils.isEmpty(user.getId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //user_fan 粉丝管理表 user_follow 关注表
        //将数据存入redis里 设置key value 时间戳 TODO 如果用户（我）的id和文章的id是同一个人 那么就不能关注
        if (dto.getOperation() == 0) {
            //关注
            //把作者的信息放进用户(我)的关注表
            cacheService.hPut(BehaviorConstants.APUSER_FOLLOW_RELATION + dto.getArticleId() + "-" + user.getId() + "", dto.getAuthorId() + "", dto.toString());

            //把用户(我)放进作者的粉丝表
            cacheService.hPut(BehaviorConstants.APUSER_FANS_RELATION + dto.getArticleId() + "-" + dto.getAuthorId() + "", user.getId() + "", dto.toString());
        } else {
            //取消关注
            cacheService.hDelete(BehaviorConstants.APUSER_FOLLOW_RELATION + dto.getArticleId() + "-" + user.getId() + "", dto.getAuthorId() + "");
            cacheService.hDelete(BehaviorConstants.APUSER_FANS_RELATION + dto.getArticleId() + "-" + dto.getAuthorId() + "", user.getId() + "");
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);



     /*   //user_fan 粉丝管理表 user_follow 关注表
        //我们获取到作者id 就要把它存进 关注表里 同时这个作者的粉丝表也要添加数据（传userId）
        Integer authorId = dto.getAuthorId();

        //该用户的关注表
        ApUserFollow apUserFollow = new ApUserFollow();
        ApUser apUser = apUserMapper.selectById(userId);
        //关注
        if (dto.getOperation()==0){
            //把作者的信息放进用户的关注表
            apUserFollow.setUserId(userId);
            apUserFollow.setFollowName(apUser.getName());
            apUserFollow.setFollowId(authorId);
            apUserFollowMapper.insert(apUserFollow);

            //把用户放进作者的粉丝表
            ApUserFan apUserFan = new ApUserFan();
            apUserFan.setUserId(authorId);
            apUserFan.setFansId(userId);
            apUserFan.setFansName(apUser.getName());
            apUserFanMapper.insert(apUserFan);
        }else {
            apUserFanMapper.deleteById(authorId);
            apUserFollowMapper.deleteById(userId);
        }


//        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);*/
    }
}
